package com.whfc.ms.api.mach.controller.esgcc;

import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.common.validator.FieldScope;
import com.whfc.common.validator.FieldScopeType;
import com.whfc.fuum.entity.SysUser;
import com.whfc.mach.dto.AppMachGpsDTO;
import com.whfc.mach.dto.obd.ObdDataDTO;
import com.whfc.mach.dto.obd.ObdMileDataDTO;
import com.whfc.mach.dto.obd.ObdOilDataDTO;
import com.whfc.mach.dto.obd.ObdParamDTO;
import com.whfc.ms.api.mach.service.MsObdDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * obd设备接口
 * @author sun_guodong
 * @date 2020-12-22
 */
@RestController
@RequestMapping("/ms/api/esgcc/mach/obd")
@Validated
public class MsEsgccObdDataController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsObdDataService msObdDataService;

    @GetMapping("/gps/log")
    public Result gpsLog(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("machId") @FieldScope(FieldScopeType.MACH_ID) Integer machId,
                         @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("获取obd设备的运动轨迹,loginer:{},machId:{},date:{}", user.getUsername(), machId, date);
        AppMachGpsDTO data = msObdDataService.gpsLog(machId, date);
        return ResultUtil.success(data);
    }

    @GetMapping("/param/list")
    public Result paramList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("machId") @FieldScope(FieldScopeType.MACH_ID) Integer machId) {
        logger.info("获取obd设备的参数列表,loginer:{},machId:{}", user.getUsername(), machId);
        ObdParamDTO data = msObdDataService.paramList(machId);
        return ResultUtil.success(data);
    }


    @GetMapping(value = "/oil/data", params = {"startDate", "endDate"})
    public Result oilData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("machId") @FieldScope(FieldScopeType.MACH_ID) Integer machId,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                          @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate) {
        logger.info("获取obd设备的多天油耗数据,loginer:{},machId:{},startDate:{},endDate:{}", user.getUsername(), machId, startDate, endDate);
        ObdOilDataDTO data = msObdDataService.oilData(machId, startDate, endDate);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/oil/data", params = {"month"})
    public Result getOilData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("machId") @FieldScope(FieldScopeType.MACH_ID) Integer machId,
                             @DateTimeFormat(pattern = "yyyy-MM") @RequestParam("month") Date month) {
        logger.info("获取obd设备的一月油耗数据,loginer:{},machId:{},month:{}", user.getUsername(), machId, month);
        Date startDate = DateUtil.getMonthBegin(month);
        Date endDate = DateUtil.getMonthEnd(month);
        ObdOilDataDTO data = msObdDataService.oilData(machId, startDate, endDate);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/oil/data", params = {"date"})
    public Result getDayOilData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("machId") @FieldScope(FieldScopeType.MACH_ID) Integer machId,
                                @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("获取obd设备的一天油耗数据,loginer:{},machId:{},date:{}", user.getUsername(), machId, date);
        ObdOilDataDTO data = msObdDataService.oilData(machId, date);
        return ResultUtil.success(data);
    }


    @GetMapping(value = "/mile/data", params = {"startDate", "endDate"})
    public Result mileData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("machId") @FieldScope(FieldScopeType.MACH_ID) Integer machId,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("startDate") Date startDate,
                           @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("endDate") Date endDate) {
        logger.info("获取obd设备的多天放线里程数据,loginer:{},machId:{},startDate:{},endDate:{}", user.getUsername(), machId, startDate, endDate);
        ObdMileDataDTO data = msObdDataService.mileData(machId, startDate, endDate);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/mile/data", params = {"month"})
    public Result getMileData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("machId") @FieldScope(FieldScopeType.MACH_ID) Integer machId,
                              @DateTimeFormat(pattern = "yyyy-MM") @RequestParam("month") Date month) {
        logger.info("获取obd设备的一月放线里程数据,loginer:{},machId:{},month:{}", user.getUsername(), machId, month);
        Date startDate = DateUtil.getMonthBegin(month);
        Date endDate = DateUtil.getMonthEnd(month);
        ObdMileDataDTO data = msObdDataService.mileData(machId, startDate, endDate);
        return ResultUtil.success(data);
    }

    @GetMapping(value = "/mile/data", params = {"date"})
    public Result getDayMileData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam("machId") @FieldScope(FieldScopeType.MACH_ID) Integer machId,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        logger.info("获取obd设备的一天放线里程数据,loginer:{},machId:{},date:{}", user.getUsername(), machId, date);
        ObdMileDataDTO data = msObdDataService.mileData(machId, date);
        return ResultUtil.success(data);
    }


    @GetMapping("/data")
    public Result getObdData(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("machId") @FieldScope(FieldScopeType.MACH_ID) Integer machId) {
        logger.info("设备obd信息,loginer:{},machId:{}", user.getUsername(), machId);
        ObdDataDTO data = msObdDataService.getObdData(machId);
        return ResultUtil.success(data);
    }


}
